<?php
namespace Home\Controller;

class PublicController extends ParentController {
    protected function _initialize() {
        $this->checkLanguage();
    }

    /**
     * 图片验证码接口
     * @modify 彪明 2016-04-27    创建
     */
    public function imageCode() {
        createVcode();
    }

    /**
     * 用户登录
     * @modify trainyao 2016-04-21    创建
     * @modify wenhui 2017-05-04 增加国际手机检验
     */
    public function login() {
        if (parent::isLogin()) {
            returnJson(403, 303);
            return;
        }

        $param = I('post.');
        $validateRule = array(
            'mobile' => 'notnull,notempty',
            'password' => 'notnull,notempty,password',
        );
        $areaCode = I('post.areaCode', '86', 'string');
        try {
            \Org\Util\Validate::validate($param, $validateRule);
            \Org\Util\Validate::validate($param['mobile'], 'globalMobile', array('globalMobile' => array('function', 'checkGlobalMobile,' . $areaCode, '403,130,error')));
        } catch (\Exception $e) {
            returnJson($e->getMessage());
            return;
        }

        // 执行登录
        $mobile = $param['mobile'];
        $password = $param['password'];
        $userLogic = A('Common/User', 'Logic');
        try {
            $userInfo = $userLogic->login($mobile, $password, $areaCode);
        } catch (\Exception $e) {
            returnJson($e->getMessage());
            return;
        }
        session(C('USER_AREACODE'), $userInfo['areacode']);
        session(C('USER_ID'), $userInfo['id']);
        session(C('USER_MOBILE'), $userInfo['mobile']);
        session(C('USER_NAME'), $userInfo['name']);

        returnJson(200, 200, array(
            'name' => $userInfo['name'],
            'mobile' => $userInfo['mobile'],
            'head' => $userInfo['headurl'],
        ));
    }

    /**
     * 发送短信登录验证码
     * @modify trainyao 2016-04-27    创建
     * @modify wenhui 2017-05-04 增加国际手机检验
     */
    public function smsLoginVcode() {
        if (parent::isLogin()) {
            returnJson(403, 303);
            return;
        }

        $param = I('post.');
        $areaCode = I('post.areaCode', '86', 'string');
        try {
            //\Org\Util\Validate::validate($param, $validateRule);
            \Org\Util\Validate::validate($param['mobile'], 'globalMobile', array('globalMobile' => array('function', 'checkGlobalMobile,' . $areaCode, '403,130,error')));

        } catch (\Exception $e) {
            returnJson($e->getMessage());
            return;
        }

        // 校验图片验证码
        $verify = $param['verify'];
        if (empty(checkVcode($verify))) {
            returnJson(403, 203);
            return;
        }
        // 发送短信验证码
        $mobile = $param['mobile'];
        try {
            $this->sendSmsVcode($mobile, 'SMSLOGIN', $areaCode);
        } catch (\Exception $e) {
            returnJson($e->getMessage());
            return;
        }

        session(C('SMSLOGIN_MOBILE'), $mobile);

        returnJson(200, 200);
    }

    /**
     * 短信验证码登录
     * @modify trainyao 2016-04-21    创建
     * @modify wenhui 2017-03-30   把账号同步到钥匙包
     * @modify wenhui 2017-05-04 增加国际手机检验
     */
    public function smsLogin() {
        if (parent::isLogin()) {
            returnJson(403, 303);
            return;
        }

        $param = I('post.');
        $validateRule = array(
            //'mobile' => 'notnull,notempty,mobile',
            'vcode' => 'notnull,notempty,vcode',
            'name' => 'length20',
        );
        $areaCode = isset($param['areaCode']) ? $param['areaCode'] : null;
        try {
            \Org\Util\Validate::validate($param, $validateRule);
            \Org\Util\Validate::validate($param['mobile'], 'globalMobile', array('globalMobile' => array('function', 'checkGlobalMobile,' . $areaCode, '403,130,error')));
        } catch (\Exception $e) {
            returnJson($e->getMessage());
            return;
        }

        // 校验传过来的手机号
        $mobile = $param['mobile'];
        $mobileInSession = session(C('SMSLOGIN_MOBILE'));
        if ($mobile != $mobileInSession) {
            returnJson(403, 100);
            return;
        }

        // 校验短信验证码是否有效
        $vcode = $param['vcode'];
        try {
            $this->checkSmsVcode($vcode, 'SMSLOGIN');
        } catch (\Exception $e) {
            returnJson($e->getMessage());
            return;
        }

        // 查询用户是否存在,存在执行登录,不存在自动注册
        $userLogic = A('Common/User', 'Logic');
        $userInfo = $userLogic->checkUserExist($mobile, $areaCode);
        try {
            if (empty($userInfo)) {
                $name = isset($param['name']) ? $param['name'] : '';
                $userLogic->register($mobile, '', $name, $areaCode);
                // 注册后登录
                $userInfo = $userLogic->login($mobile, null, $areaCode);
                // 把账号同步到钥匙包
                $userLogic->qrclientRegister($mobile, $name, $areaCode);
            } else {
                // 执行登录
                $userInfo = $userLogic->login($mobile, null, $areaCode);
            }
        } catch (\Exception $e) {
            returnJson($e->getMessage());
            return;
        }

        session(C('SMSLOGIN_VALIDATE_CODE'), null);
        session(C('SMSLOGIN_VALIDATE_CODE_SEND_TIME'), null);

        session(C('USER_AREACODE'), $userInfo['areacode']);
        session(C('USER_ID'), $userInfo['id']);
        session(C('USER_MOBILE'), $userInfo['mobile']);
        session(C('USER_NAME'), $userInfo['name']);

        returnJson(200, 200, array(
            'areaCode' => $userInfo['areacode'],
            'name' => $userInfo['name'],
            'mobile' => $userInfo['mobile'],
            'head' => $userInfo['headurl'],
        ));
    }

    /**
     * 用户注册前检查,发送
     * @modify trainyao 2016-04-27    创建
     * @modify wenhui 2017-05-04 增加国际手机检验
     */
    public function checkRegister() {
        if (parent::isLogin()) {
            returnJson(403, 303);
            return;
        }

        $param = I('post.');
        $validateRule = array(
            //'mobile' => 'notnull,notempty,mobile',
            'imageCode' => 'notnull,notempty,imageCode',
        );
        $areaCode = I('post.areaCode', '86', 'string');
        try {
            \Org\Util\Validate::validate($param, $validateRule);
            \Org\Util\Validate::validate($param['mobile'], 'globalMobile', array('globalMobile' => array('function', 'checkGlobalMobile,' . $areaCode, '403,130,error')));
        } catch (\Exception $e) {
            returnJson($e->getMessage());
            return;
        }

        // 校验图片验证码
        $imageCode = $param['imageCode'];
        if (empty(checkVcode($imageCode))) {
            returnJson(403, 203);
            return;
        }

        // 校验用户是否存在
        $mobile = $param['mobile'];
        $userLogic = A('Common/User', 'Logic');
        $userExist = $userLogic->checkUserExist($mobile, $areaCode);
        if (!empty($userExist)) {
            returnJson(403, 301);
            return;
        }

        returnJson(200, 200);
    }

    /**
     * 发送注册验证码
     * @modify trainyao 2016-04-27    创建
     * @modify wenhui 2017-05-04 增加国际手机检验
     */
    public function registerVcode() {
        if (parent::isLogin()) {
            returnJson(403, 303);
            return;
        }

        $param = I('post.');
        $areaCode = I('post.areaCode', '86', 'string');
        try {
            \Org\Util\Validate::validate($param['mobile'], 'globalMobile', array('globalMobile' => array('function', 'checkGlobalMobile,' . $areaCode, '403,130,error')));
        } catch (\Exception $e) {
            returnJson($e->getMessage());
            return;
        }

        // 用户不存在报错
        $mobile = $param['mobile'];
        $userLogic = A('Common/User', 'Logic');
        $userInfo = $userLogic->checkUserExist($mobile, $areaCode);
        if (!empty($userInfo)) {
            returnJson(403, 301);
            return;
        }

        // 发送短信验证码
        try {
            $this->sendSmsVcode($mobile, 'REGISTER', $areaCode);
        } catch (\Exception $e) {
            returnJson($e->getMessage());
            return;
        }

        session(C('REGISTER_MOBILE'), $mobile);
        returnJson(200, 200);
    }

    /**
     * 用户注册
     * @modify trainyao 2016-04-21    创建
     * @modify wenhui 2017-03-30   把账号同步到钥匙包
     * @modify wenhui 2017-05-04 增加国际手机检验
     */
    public function register() {
        if (parent::isLogin()) {
            returnJson(403, 303);
            return;
        }

        $param = I('post.');
        $validateRule = array(
            //'mobile' => 'notnull,notempty,mobile',
            'password' => 'notnull,notempty,password',
            'vcode' => 'notnull,notempty,vcode',
        );
        $areaCode = I('post.areaCode', '86', 'string');
        try {
            \Org\Util\Validate::validate($param, $validateRule);
            \Org\Util\Validate::validate($param['mobile'], 'globalMobile', array('globalMobile' => array('function', 'checkGlobalMobile,' . $areaCode, '403,130,error')));
        } catch (\Exception $e) {
            returnJson($e->getMessage());
            return;
        }

        // 校验传过来手机号
        $mobile = $param['mobile'];
        $mobileInSession = session(C('REGISTER_MOBILE'));
        if (empty($mobileInSession) || $mobile != $mobileInSession) {
            returnJson(403, 100);
            return;
        }

        // 校验验证码,session是否有值,验证码是否已经过期
        $vcode = $param['vcode'];
        try {
            $this->checkSmsVcode($vcode, 'REGISTER');
        } catch (\Exception $e) {
            returnJson($e->getMessage());
            return;
        }

        // 执行注册(注册入口姓名注册为手机号)
        $name = $mobile;
        $password = $param['password'];
        $userLogic = A('Common/User', 'Logic');
        try {
            $registerResult = $userLogic->register($mobile, $password, $name, $areaCode);
        } catch (\Exception $e) {
            returnJson($e->getMessage());
            return;
        }

        // 把账号同步到钥匙包
        $userLogic->qrclientRegister($mobile, $name, $areaCode);

        session(C('REGISTER_VALIDATE_CODE'), null);
        session(C('REGISTER_VALIDATE_CODE_SEND_TIME'), null);
        session(C('REGISTER_MOBILE'), null);

        returnJson(200, 200);
    }

    /**
     * 用户注册前检查,发送
     * @modify trainyao 2016-04-27    创建
     * @modify wenhui 2017-05-04 增加国际手机检验
     */
    public function checkForgetPassword() {
        if (parent::isLogin()) {
            returnJson(403, 303);
            return;
        }

        $param = I('post.');
        $validateRule = array(
            //'mobile' => 'notnull,notempty,mobile',
            'imageCode' => 'notnull,notempty,imageCode',
        );
        $areaCode = I('post.areaCode', '86', 'string');
        try {
            \Org\Util\Validate::validate($param, $validateRule);
            \Org\Util\Validate::validate($param['mobile'], 'globalMobile', array('globalMobile' => array('function', 'checkGlobalMobile,' . $areaCode, '403,130,error')));
        } catch (\Exception $e) {
            returnJson($e->getMessage());
            return;
        }

        // 校验图片验证码
        $imageCode = $param['imageCode'];
        if (empty(checkVcode($imageCode))) {
            returnJson(403, 203);
            return;
        }

        // 校验用户是否存在
        $mobile = $param['mobile'];
        $userLogic = A('Common/User', 'Logic');
        $userExist = $userLogic->checkUserExist($mobile, $areaCode);
        if (empty($userExist)) {
            returnJson(403, 300);
            return;
        }

        returnJson(200, 200);
    }

    /**
     * 发送忘记密码验证码
     * @modify trainyao 2016-04-27    创建
     * @modify wenhui 2017-05-04 增加国际手机检验
     */
    public function forgetPasswordVcode() {
        if (parent::isLogin()) {
            returnJson(403, 303);
            return;
        }

        $param = I('post.');
        $areaCode = I('post.areaCode', '86', 'string');
        try {
            \Org\Util\Validate::validate($param['mobile'], 'globalMobile', array('globalMobile' => array('function', 'checkGlobalMobile,' . $areaCode, '403,130,error')));
        } catch (\Exception $e) {
            returnJson($e->getMessage());
            return;
        }

        // 用户不存在报错
        $mobile = $param['mobile'];
        $userLogic = A('Common/User', 'Logic');
        $userInfo = $userLogic->checkUserExist($mobile, $areaCode);
        if (empty($userInfo)) {
            returnJson(403, 300);
            return;
        }

        // 发送短信验证码
        try {
            $this->sendSmsVcode($mobile, 'FORGET_PASSWORD', $areaCode);
        } catch (\Exception $e) {
            returnJson($e->getMessage());
            return;
        }

        session(C('FORGET_PASSWORD_MOBILE'), $mobile);
        returnJson(200, 200);
    }

    /**
     * 用户忘记密码修改密码
     * @modify trainyao 2016-04-21    创建
     * @modify wenhui 2017-05-04 增加国际手机检验
     */
    public function forgetPassword() {
        if (parent::isLogin()) {
            returnJson(403, 303);
            return;
        }

        $param = I('post.');
        $validateRule = array(
            //'mobile' => 'notnull,notempty,mobile',
            'password' => 'notnull,notempty,password',
            'vcode' => 'notnull,notempty,vcode',
        );
        $areaCode = I('post.areaCode', '86', 'string');
        try {
            \Org\Util\Validate::validate($param, $validateRule);
            \Org\Util\Validate::validate($param['mobile'], 'globalMobile', array('globalMobile' => array('function', 'checkGlobalMobile,' . $areaCode, '403,130,error')));
        } catch (\Exception $e) {
            returnJson($e->getMessage());
            return;
        }

        // 校验验证码,session是否有值,验证码是否已经过期
        $vcode = $param['vcode'];
        try {
            $this->checkSmsVcode($vcode, 'FORGET_PASSWORD');
        } catch (\Exception $e) {
            returnJson($e->getMessage());
            return;
        }

        // 校验传过来手机号
        $mobile = $param['mobile'];
        $mobileInSession = session(C('FORGET_PASSWORD_MOBILE'));
        if (empty($mobileInSession) || $mobile != $mobileInSession) {
            returnJson(403, 100);
            return;
        }

        // 检查用户是否存在
        $password = $param['password'];
        $userLogic = A('Common/User', 'Logic');
        try {
            $forgetPasswordResult = $userLogic->savePassword($mobile, $password, $areaCode);
        } catch (\Exception $e) {
            returnJson($e->getMessage());
            return;
        }

        session(C('FORGET_PASSWORD_VALIDATE_CODE'), null);
        session(C('FORGET_PASSWORD_VALIDATE_CODE_SEND_TIME'), null);
        session(C('FORGET_PASSWORD_MOBILE'), null);

        returnJson(200, 200);
    }

    /**
     * 退出登录接口
     * @modify trainyao 2016-04-27    创建
     */
    public function logout() {
        if (!parent::isLogin()) {
            returnJson(403, 304);
            return;
        }

        parent::logout();
        returnJson(200, 200);
    }

    /**
     * 发送短信验证码统一逻辑
     * 方法会抛异常
     * @modify trainyao 2016-04-27    创建
     * @modify wenhui 2011-05-04    添加区号
     * @param  string   $mobile       手机号
     * @param  string   $configPrefix 配置项索引
     * @param  string   $areaCode 手机区号
     */
    private function sendSmsVcode($mobile, $configPrefix, $areaCode = '86') {
        // 校验1分钟内是否已经发送过
        $sendTime = session(C($configPrefix . '_VALIDATE_CODE_SEND_TIME'));
        if (!empty($sendTime) && !isOverMinute($sendTime, C('VALIDATE_CODE_RESENT_MINUTE'))) {
            throw new \Exception('403,200', 200);
            return;
        }

        // 生成随机数字发送
        $vcode = getRandomNumber(4);
        if ($areaCode == '86') {
            $smsUtil = new \Org\Util\Sms();
            $smsSendResult = $smsUtil->sendVocode($mobile, $vcode);
        } else {
            $smsUtil = new \Org\Util\yunPian();
            $smsSendResult = $smsUtil->sendVcode($areaCode, $mobile, $vcode);
        }

        if (empty($smsSendResult)) {
            throw new \Exception('500,507', 507);
            return;
        }

        $time = time();
        session(C($configPrefix . '_VALIDATE_CODE'), $vcode);
        session(C($configPrefix . '_VALIDATE_CODE_SEND_TIME'), $time);
    }

    /**
     * 校验短信验证码逻辑
     * 方法会抛异常
     * @modify trainyao 2016-04-27 创建
     * @param  string   $vcode     短信验证码
     * @param  string   $configPrefix     配置项前缀,用于检测不同功能模块
     * @return mixin                校验失败抛异常
     */
    private function checkSmsVcode($vcode, $configPrefix) {
        // 校验验证码,session是否有值,验证码是否已经过期
        $vcodeInSession = session(C($configPrefix . '_VALIDATE_CODE'));
        $vcodeSendTime = session(C($configPrefix . '_VALIDATE_CODE_SEND_TIME'));
        if (empty($vcodeInSession) || empty($vcodeSendTime)) {
            throw new \Exception('403,202', 202);
            return;
        }
        if (isOverMinute($vcodeSendTime, C('VALIDATE_CODE_OUTDATE_MINUTE'))) {
            throw new \Exception('403,201', 201);
            return;
        }
        if ($vcode != $vcodeInSession) {
            throw new \Exception('403,202', 202);
            return;
        }
    }

    /**
     * 钥匙包发现模块跳转微预定 自动登录
     * @modify wenhui 2017-04-10 创建
     */
    public function autoLogin() {
        $param = I('post.');
        $userId = $param['userId'];
        $token = $param['token'];

        // rpc 验证钥匙包id token
        Vendor('phpRPC.phprpc_client');
        $client = new \PHPRPC_Client(C('QRCLIENTSYNC_RPC_URL'));
        $RPCData = array(
            'userId' => $userId,
            'token' => $token,
        );
        $result = $client->autoLogin($RPCData);
        if (!$result) {
            returnJson(403, 100);
            return;
        }

        $webUserId = $result['webUserId'];
        $userloginModel = D('UserLogin');
        $userInfo = $userloginModel->getInfo($webUserId);
        if ($userInfo['isdelete'] == 1) {
            returnJson(403, 300);
            return;
        }

        $name = empty($userInfo['name']) ? $userInfo['mobile'] : $userInfo['name'];
        if (is_null(session(C('USER_ID')))) {
            session(C('USER_AREACODE'), $userInfo['areacode']);
            session(C('USER_ID'), $webUserId);
            session(C('USER_NAME'), $name);
            session(C('USER_MOBILE'), $userInfo['mobile']);
        } else if (session(C('USER_ID')) != $webUserId) {
            session(C('USER_AREACODE'), $userInfo['areacode']);
            session(C('USER_ID'), $webUserId);
            session(C('USER_NAME'), $name);
            session(C('USER_MOBILE'), $userInfo['mobile']);
        }

        returnJson(200, 200, array(
            'name' => $userInfo['name'],
            'areacode' => $userInfo['areacode'],
            'mobile' => $userInfo['mobile'],
            'head' => $userInfo['headurl'],
        ));
    }

    /**
     * 获取客栈提示优惠券
     * @modify wenhui 2018-01-08    创建
     */
    public function getHotelTipCoupon() {
        $param = I('post.');
        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
        );
        try {
            \Org\Util\Validate::validate($param, $validateRule);
        } catch (\Exception $e) {
            returnJson($e->getMessage());
            return;
        }

        $userId = getUserId();
        // 调用RPC获取信息
        Vendor('phpRPC.phprpc_client');
        $client = new \PHPRPC_Client(C('HOTEL_RPC_URL'));
        $client->setEncryptMode(3);
        $RPCData = array(
            'flag' => C('MEIZHU_RPC_FLAG'),
            'time' => time(),
            'hotelId' => $param['hotel'],
            'userId' => $userId,
            'bookLangSet' => LANG_SET,
            'status' => 1,
        );
        $result = $client->getCoupon($RPCData);
        if (!is_array($result)) {
            returnJson(500, 500);
            return;
        }

        // 根据不同情况返回信息
        if ($result['httpstatus'] == 200) {
            $date = array();
            if (!empty($result['data'])) {
                $couponDate = my_sort($result['data'], 'createtime', SORT_DESC, SORT_NUMERIC);
                $date = array(
                    'couponName' => $couponDate[0]['couponName'],
                    'couponNo' => $couponDate[0]['couponNo'],
                    'couponType' => $couponDate[0]['couponType'],
                    'couponPrice' => $couponDate[0]['couponPrice'],
                    'couponDiscount' => $couponDate[0]['couponDiscount'],
                    'couponMinusPrice' => $couponDate[0]['couponMinusPrice'],
                );

                // 是否提示过
                if (isset($_COOKIE['coupon_cookie'])) {
                    if ($_COOKIE['coupon_cookie'] == $date['couponNo']) {
                        $date = array();
                    }
                }
            }

            returnJson(200, 200, $date);
            return;
        }

        returnJson(403, 101, $result['httpstatus'] . $result['status'], null, null, $result['info']);
    }

    /**
     * 领取优惠券并短信验证码登录
     * @modify wenhui 2018-01-10    创建
     */
    public function getCouponAndSmsLogin() {
        // 微信登录 没有绑定手机号 即 USER_ID = 0  算没有登录 需要绑定再领取优惠券
        $userId = session(C('USER_ID'));
        if (parent::isLogin() && $userId != 0) {
            returnJson(403, 303);
            return;
        }

        $param = I('post.');
        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'couponNo' => 'notnull,notempty,cpOrder',
            'vcode' => 'notnull,notempty,vcode',
        );
        $areaCode = isset($param['areaCode']) ? $param['areaCode'] : null;
        try {
            \Org\Util\Validate::validate($param, $validateRule);
            \Org\Util\Validate::validate($param['mobile'], 'globalMobile', array('globalMobile' => array('function', 'checkGlobalMobile,' . $areaCode, '403,130,error')));
        } catch (\Exception $e) {
            returnJson($e->getMessage());
            return;
        }

        // 校验传过来的手机号
        $mobile = $param['mobile'];
        $mobileInSession = session(C('SMSLOGIN_MOBILE'));
        if ($mobile != $mobileInSession) {
            returnJson(403, 100);
            return;
        }

        // 校验短信验证码是否有效
        $vcode = $param['vcode'];
        try {
            $this->checkSmsVcode($vcode, 'SMSLOGIN');
        } catch (\Exception $e) {
            returnJson($e->getMessage());
            return;
        }

        $hotel = $param['hotel'];
        // 查询用户是否存在,存在执行登录,不存在自动注册
        $userLogic = A('Common/User', 'Logic');
        $userInfo = $userLogic->checkUserExist($mobile, $areaCode);
        try {
            if (empty($userInfo)) {
                // 微信登录 获取微信返回的名称
                $name = !is_null($userId) && $userId == 0 ? session(C('USER_NAME')) : '';
                $string = chr(mt_rand(97,122));
                $password = $string . getRandomNumber(5);
                $userLogic->register($mobile, $password, $name, $areaCode);
                // 注册后登录
                $userInfo = $userLogic->login($mobile, null, $areaCode);
                // 把账号同步到钥匙包
                $userLogic->qrclientRegister($mobile, $name, $areaCode);

                // 注册成功 并发短信 微信登录不发短信
                if (is_null($userId)) {
                    $this->sendRegisterSms($hotel, $areaCode, $mobile, $password);
                }
            } else {
                // 执行登录
                $userInfo = $userLogic->login($mobile, null, $areaCode);
            }
        } catch (\Exception $e) {
            returnJson($e->getMessage());
            return;
        }

        // 微信登录并且未绑定手机号需要绑定
        $wechatAndNotBindMobile = session(C('AUTH_LOGIN_TYPE')) == USER_OAUTH_TYPE_WECHAT_WEB && $userId == 0;
        if ($wechatAndNotBindMobile) {
            $userLogic->bindUser($userInfo['id'], session(C('WECHAT_LOGIN_OPEN_ID')), USER_OAUTH_TYPE_WECHAT_WEB);
        }

        // 调用RPC领取优惠券
        Vendor('phpRPC.phprpc_client');
        $client = new \PHPRPC_Client(C('HOTEL_RPC_URL'));
        $client->setEncryptMode(3);
        $RPCData = array(
            'flag' => C('MEIZHU_RPC_FLAG'),
            'time' => time(),
            'hotelId' => $hotel,
            'couponNo' => $param['couponNo'],
            'userId' => $userInfo['id'],
        );
        $result = $client->addUserCoupons($RPCData);
        if (!is_array($result)) {
            returnJson(500, 500);
            return;
        }

        session(C('SMSLOGIN_VALIDATE_CODE'), null);
        session(C('SMSLOGIN_VALIDATE_CODE_SEND_TIME'), null);
        session(C('USER_ID'), null);
        session(C('USER_NAME'), null);
        session(C('USER_AREACODE'), null);
        session(C('USER_MOBILE'), null);
        session(C('WECHAT_OAUTH_LOGIN'), null);
        session(C('WECHAT_LOGIN_OPEN_ID'), null);
        session(C('AUTH_LOGIN_TYPE'), null);

        session(C('USER_AREACODE'), $userInfo['areacode']);
        session(C('USER_ID'), $userInfo['id']);
        session(C('USER_MOBILE'), $userInfo['mobile']);
        session(C('USER_NAME'), $userInfo['name']);

        // 根据不同情况返回信息
        if ($result['httpstatus'] != 200) {
            returnJson(403, 101, $result['httpstatus'] . $result['status'], null, null, $result['info']);
            return;
        }

        returnJson(200, 200, array(
            'areaCode' => $userInfo['areacode'],
            'name' => $userInfo['name'],
            'mobile' => $userInfo['mobile'],
            'head' => $userInfo['headurl'],
        ));
    }

    /**
     * 发送短信登录验证码
     * @modify trainyao 2016-04-27    创建
     * @modify wenhui 2017-05-04 增加国际手机检验
     */
    public function smsCouponLoginVcode() {
        // session(C('USER_ID')) = 0 即 微信登录没有绑定手机号
        if (parent::isLogin() && session(C('USER_ID')) != 0) {
            returnJson(403, 303);
            return;
        }

        $param = I('post.');
        $areaCode = I('post.areaCode', '86', 'string');
        try {
            //\Org\Util\Validate::validate($param, $validateRule);
            \Org\Util\Validate::validate($param['mobile'], 'globalMobile', array('globalMobile' => array('function', 'checkGlobalMobile,' . $areaCode, '403,130,error')));

        } catch (\Exception $e) {
            returnJson($e->getMessage());
            return;
        }

        // 发送短信验证码
        $mobile = $param['mobile'];
        try {
            $this->sendSmsVcode($mobile, 'SMSLOGIN', $areaCode);
        } catch (\Exception $e) {
            returnJson($e->getMessage());
            return;
        }

        session(C('SMSLOGIN_MOBILE'), $mobile);

        returnJson(200, 200);
    }

    /**
     * 发送短信
     * @modify wenhui 2018-01-10    创建
     * @param  string   $hotel     客栈id
     * @param  string   $areaCode  区号
     * @param  string   $mobile  发送号码
     * @param  string   $password  发送密码
     */
    private function sendRegisterSms($hotel, $areaCode, $mobile, $password) {
        $loginLink = "http://" . MEIZHU_BOOK_HOST . "/Home/BookPage/index?hotel={$hotel}";
        $loginLink = shortenUrl($loginLink);
        if ($areaCode == '86') {
            $smsUtil = new \Org\Util\Sms();
            $smsSendResult = $smsUtil->sendNotRegBook($mobile, $password, $loginLink);
        } else {
            $smsUtil = new \Org\Util\yunPian();
            $smsSendResult = $smsUtil->yunPianSendNotRegBook($areaCode, $mobile, $password, $loginLink);
        }
        return;
    }
}
